import { Chat } from "../Chat";
import { ui } from "../Chat/ui";
import { IRawMsg } from "../../chatlog_types/IRawMsg";
import { MsgSubTypeEnum } from "../../chatlog_types/MsgSubTypeEnum";
import { IRawMsgContent_File, IRawMsgContent_Refer } from "@/chatlog_types/IRawMsgContents";
import styles from "./styles.module.scss"
export namespace RawMsgShareType {
  function info(text: string, desc: string = text): Info { return { text, desc }; }
  export interface Info {
    text: string;
    desc: string;
  }
  export const Value = MsgSubTypeEnum;
  export type Value = MsgSubTypeEnum;
  export const Infos: Record<Value, Info> = {
    [Value.Link]: info('链接'),
    [Value.File]: info('文件'),
    [Value.GIF]: info('GIF表情'),
    [Value._19]: info('转发'),
    [Value.Minipro33]: info('小程序'),
    [Value.Minipro36]: info('小程序'),
    [Value._51]: info('视频号'),
    [Value.Refer]: info('引用'),
    [Value._62]: info('62'),
    [Value._63]: info('视频号'),
    [Value._87]: info('群公告'),
    [Value._2000]: info('2000'),
    [Value._2001]: info('红包'),
    [Value._2003]: info('红包封面')
  };
  export const readType = (msg: IRawMsg): Chat.MessageType => {
    switch (msg.subType as Value) {
      case Value.Refer:
        return Chat.MessageType.text
      case Value.Link:
        return Chat.MessageType.link
      case Value.File:
        return Chat.MessageType.file
      case Value.GIF:
      case Value._19:
      case Value.Minipro33:
      case Value.Minipro36:
      case Value._51:
      case Value._62:
      case Value._63:
      case Value._87:
      case Value._2000:
      case Value._2001:
      case Value._2003:
    }
    return Chat.MessageType.text
  }
  export const readContent = (msg: IRawMsg): string => {
    switch (msg.subType as Value) {
      case Value.Refer: return msg.content || '';
      case Value.File: {
        const c = msg.contents as Unsafe<IRawMsgContent_File>;
        if (!c) break;
        return `[文件] ${c.title}`
      }
    }
    const sub_type_name =
      Infos[msg.subType as Value]?.text ??
      Value[msg.subType as Value] ??
      `sub_type_${msg.subType}`
    return `[${sub_type_name}] ${JSON.stringify(msg)}`;
  }
  export const renderAfterBubble = (msg: IRawMsg | undefined | null, children?: ui.UINode[] | ui.UINode): ui.UINode[] | ui.UINode => {
    if (!msg) return children
    switch (msg.subType) {
      case Value.Refer: {
        const contents = msg.contents as Unsafe<IRawMsgContent_Refer>
        const refer_ele = contents?.refer ? ui.node('div', {
          className: msg.isSelf ? styles.refer_box_self : styles.refer_box,
          onclick: (e) => {
            const ele = e.target as HTMLDivElement;
            if (ele.classList.contains(styles.refer_box_open)) {
              ele.classList.remove(styles.refer_box_open)
              document.getSelection()?.removeAllRanges()
            } else {
              ele.classList.add(styles.refer_box_open)
              document.getSelection()?.selectAllChildren(ele)
            }
          },
        }, `${contents.refer.senderName}: ${contents.refer.content?.trim()}`
        ) : null
        return ui.node('div', { className: styles.bottom_box }, [refer_ele, children])
      }
    }
    return children
  }
}
